# -*- coding: utf-8 -*-
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker

Base = declarative_base()
isInit = False
engine = None

async def register_db(dsn="sqlite:///./app.db", echo=False):
    global isInit, SessionLocal, engine
    if isInit: return
    isInit = True
    engine = create_engine(
        dsn, connect_args={"check_same_thread": False}, echo=echo
    )
    SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
    # Base.metadata.create_all(bind=engine)

# Dependency
def get_db():
    db = SessionLocal()
    try:
        yield db
    finally:
        db.close()